package esay.array;

import java.util.Arrays;

/**
 * 找出nums中相加等于target的元素位置
 * @author ruizhou
 * @date 26.02.2018
 */
public class TwoSum {
    private static int[] twoSum(int[] nums, int target) {
        int[] copy_nums = new int[nums.length];
        System.arraycopy(nums, 0, copy_nums, 0, nums.length);
        Arrays.sort(copy_nums);

        int left = 0;
        int right = nums.length -1;

        int[] result = new int[2];
        while (left < right) {
            if (copy_nums[left] + copy_nums[right] < target) {
                left++;
            } else if (copy_nums[left] + copy_nums[right] > target) {
                right--;
            } else {
                Boolean flag = true;
                for (int i = 0, size  = nums.length; i < size; i++) {
                    if (copy_nums[left] == nums[i] && result[1] != i && flag) {
                        result[0] = i;
                        flag = false;
                    } else if (copy_nums[right] == nums[i] && result[1] != i) {
                        result[1] = i;
                    }
                }
                break;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {2,5,5,11};
        System.out.println(twoSum(nums, 10));
    }
}
